package com.onetime.mall.trading.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onetime.mall.trading.dao.OrderAddressDao;
import com.onetime.mall.trading.entity.OrderAddressEntity;
import com.onetime.mall.trading.service.OrderAddressService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.Query;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("orderAddressService")
public class OrderAddressServiceImpl extends ServiceImpl<OrderAddressDao, OrderAddressEntity> implements OrderAddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderAddressEntity> page = this.page(
                new Query<OrderAddressEntity>().getPage(params),
                new QueryWrapper<OrderAddressEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 为订单新增地址信息
     *
     * @return
     */
    @Override
    public boolean addOrderAddress(OrderAddressEntity orderAddressEntity) {
        QueryWrapper<OrderAddressEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderAddressEntity.getOrderId());
        OrderAddressEntity one = baseMapper.selectOne(wrapper);
        if (one == null) {
            return baseMapper.insert(orderAddressEntity) == 1;
        } else {
            return false;
        }

    }


    /**
     * 获取订单的地址信息
     *
     * @param orderId 订单ID
     * @return 订单类
     */
    @Override
    public OrderAddressEntity getOrderAddress(Long orderId) {
        return baseMapper.selectOne(new QueryWrapper<OrderAddressEntity>().eq("order_id", orderId));
    }

    /**
     * // TODO: 2022/3/29  更新订单的地址信息，未验证用户身份
     *
     * @return
     */
    @Override
    public boolean updateOrderAddress(OrderAddressEntity orderAddressEntity) {
        int update = baseMapper.updateById(orderAddressEntity);
        /*orderAddressEntity.setOrderId(null);*/
        return update == 1;
    }

}